package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityDishesWhite;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.pyhandle.MenuListVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoDishesWhite;
import com.ocom.payhandle.service.DishesWhiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishesWhiteServiceImpl extends ServiceImpl<IDaoDishesWhite, EntityDishesWhite> implements DishesWhiteService {


	@Autowired
	IDaoDishesWhite iDaoDishesWhite;

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
	public EntityDishesWhite getMaxId(Long comId, Integer merchId) {
		QueryWrapper<EntityDishesWhite> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("merch_id", merchId);
		queryWrapper.orderByDesc("id");
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
	public List<MenuListVo> dishesList(Long comId,
									   Integer menuAdd,
									   Integer menuDelete,
									   Integer listLimit,
									   Integer merchId) {
		return iDaoDishesWhite.getDishesList(comId,
				menuAdd,
				menuDelete,
				listLimit,
				merchId);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
	public EntityDishesWhite checkExist(Long comId, Integer merchId, Integer menuAdd, Integer menuDelete) {
		QueryWrapper<EntityDishesWhite> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("merch_id", merchId);
		queryWrapper.gt("id", menuAdd);
		queryWrapper.and(wrapper -> {
			wrapper.or().eq("state", 0).or().gt("id", menuDelete);
		});
		queryWrapper.orderByDesc("id");
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}


	/**
	 * select 	id , dishes_id
	 * from 	dishes_white
	 * where 	com_id = @companyId	and 	merch_id = @deviceInfo.menu_merch_id
	 * and 	id > @deviceInfo.menu_add
	 * and 	( state = 0 	or 	id > @deviceInfo.menu_delete	)
	 * order by	id 	limit 	1
	 */
}
